package com.songhuan.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.songhuan.web.dto.*;
import com.songhuan.web.dto.query.*;
import com.songhuan.web.entity.*;
import com.songhuan.web.mapper.*;
import com.songhuan.web.service.*;
import com.songhuan.web.tools.dto.*;
import com.songhuan.web.tools.Extension;
import com.songhuan.web.tools.exception.CustomException;
import com.songhuan.web.dto.AppUserDto;
import com.songhuan.web.dto.CheckSettingDto;
import com.songhuan.web.dto.OrganizationDto;
import com.songhuan.web.dto.query.CheckSettingPagedInput;
import com.songhuan.web.entity.AppUser;
import com.songhuan.web.entity.CheckSetting;
import com.songhuan.web.entity.Organization;
import com.songhuan.web.mapper.AppUserMapper;
import com.songhuan.web.mapper.CheckSettingMapper;
import com.songhuan.web.mapper.OrganizationMapper;
import com.songhuan.web.mapper.ReCheckMapper;
import com.songhuan.web.service.CheckSettingService;
import com.songhuan.web.tools.dto.PagedResult;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import lombok.SneakyThrows;
import com.songhuan.web.tools.*;
/**
 * 考勤设置功能实现类
 */
@Service
public class CheckSettingServiceImpl extends ServiceImpl<CheckSettingMapper, CheckSetting> implements CheckSettingService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的CheckSetting表mapper对象
     */
    @Autowired
    private CheckSettingMapper _CheckSettingMpper;

 			
    @Autowired
    private OrganizationMapper _OrganizationMapper;

    /**
     * 操作数据库的ReCheck表mapper对象
     */
    @Autowired
    private ReCheckMapper _ReCheckMpper;
    /**
     * 考勤设置分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<CheckSettingDto> List(CheckSettingPagedInput input) {

      
        //声明一个支持考勤设置查询的(拉姆达)表达式
        LambdaQueryWrapper<CheckSetting> queryWrapper = Wrappers.<CheckSetting>lambdaQuery()
                .eq(input.getId()!=null&&input.getId()!=0,CheckSetting::getId,input.getId())
                .eq(input.getCreatorId()!=null,CheckSetting::getCreatorId,input.getCreatorId());
        //如果前端搜索传入查询条件则拼接查询条件
        if(input.getBeginDateRange()!=null&&input.getBeginDateRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(CheckSetting::getBeginDate,input.getBeginDateRange().get(1));
            queryWrapper=queryWrapper.gt(CheckSetting::getBeginDate,input.getBeginDateRange().get(0));
        }
        if(input.getEndDateRange()!=null&&input.getEndDateRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(CheckSetting::getEndDate,input.getEndDateRange().get(1));
            queryWrapper=queryWrapper.gt(CheckSetting::getEndDate,input.getEndDateRange().get(0));
        }

        if(Extension.isNotNullOrEmpty(input.getAmBeginHMS())) {
            queryWrapper=queryWrapper.eq(CheckSetting::getAmBeginHMS,input.getAmBeginHMS());
       	 }

        if(Extension.isNotNullOrEmpty(input.getAmEndHMS())) {
            queryWrapper=queryWrapper.eq(CheckSetting::getAmEndHMS,input.getAmEndHMS());
       	 }

        if(Extension.isNotNullOrEmpty(input.getPmBeginHMS())) {
            queryWrapper=queryWrapper.eq(CheckSetting::getPmBeginHMS,input.getPmBeginHMS());
       	 }

        if(Extension.isNotNullOrEmpty(input.getPmEndHMS())) {
            queryWrapper=queryWrapper.eq(CheckSetting::getPmEndHMS,input.getPmEndHMS());
       	 }
        if(input.getOrganizationId()!=null) {
            queryWrapper=queryWrapper.eq(CheckSetting::getOrganizationId,input.getOrganizationId());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(CheckSetting::getCreationTime);
        //构建一个分页查询的model
        Page<CheckSetting> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取考勤设置数据
        IPage<CheckSetting> pageRecords= _CheckSettingMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _CheckSettingMpper.selectCount(queryWrapper);

        //把CheckSetting实体转换成CheckSetting传输模型
        List<CheckSettingDto> items= Extension.copyBeanList(pageRecords.getRecords(),CheckSettingDto.class);
        for (CheckSettingDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto CreatorAppUserDTO=new AppUserDto();
            AppUser CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Organization表信息
           OrganizationDto OrganizationDTO = new OrganizationDto();
            Organization OrganizationEntity= _OrganizationMapper.selectOne(Wrappers.<Organization>lambdaQuery().eq(Organization::getId,item.getOrganizationId()));
           if(OrganizationEntity!=null) {  
          BeanUtils.copyProperties(OrganizationDTO,OrganizationEntity);
            item.setOrganizationDto(OrganizationDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个考勤设置查询
     */
    @SneakyThrows
    @Override
    public CheckSettingDto Get(CheckSettingPagedInput input) {
       if(input.getId()==null)
        {
            return new CheckSettingDto();
        }
        PagedResult<CheckSettingDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new CheckSettingDto();
    }

    /**
     *考勤设置创建或者修改
     */
    @SneakyThrows
    @Override
    public CheckSettingDto CreateOrEdit(CheckSettingDto input) {


        //如果起始日期大于结束日期报错
        if (input.getBeginDate().compareTo(input.getEndDate()) > 0) {
            throw new CustomException("起始日期不能大于结束日期");
        }

        //声明一个考勤设置实体
        CheckSetting CheckSetting = new CheckSetting();

        //把前端传入的input参数拷贝到考勤设置实体
        BeanUtils.copyProperties(CheckSetting,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(CheckSetting);

        //定义一个返回给前端的考勤设置传输模型
        CheckSettingDto CheckSettingDto=new CheckSettingDto();

        //同理把操作的考勤设置实体拷贝给考勤设置传输模型
        BeanUtils.copyProperties(CheckSettingDto,CheckSetting);

        //把传输模型返回给前端
        return CheckSettingDto;
    }

    @Override
    public List<CheckSetting> GetCheckCourseList(CheckSettingDto input) {

        return _CheckSettingMpper.selectList(new LambdaQueryWrapper<CheckSetting>()
                .eq(CheckSetting::getOrganizationId, input.getOrganizationId())
                .le(CheckSetting::getBeginDate, input.getBeginDate())
                .ge(CheckSetting::getEndDate, input.getBeginDate())
                .orderByAsc(CheckSetting::getAmBeginHMS)
        );
    }

    @Override
    public List<CheckSetting> GetSelectList() {
        return _CheckSettingMpper.selectList(null);
    }
}
